<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use WechatPayV3\WechatPayV3;
class WechatPayComplaint extends Controller
{
    public function mchList()
    {
//        $mch[0] = [说
//            5=>['mch_id'=>'1571615431','data'=>$this->getMchUncomplaintCount('1571615431'),'pay_uniacid'=>5],
//            12=>['mch_id'=>'1567597801','data'=>$this->getMchUncomplaintCount('1567597801'),'pay_uniacid'=>12],
//            ];
//        $mch[1] = [
//            17=>['mch_id'=>'1609226509','data'=>$this->getMchUncomplaintCount('1609226509'),'pay_uniacid'=>17],
//            24=>['mch_id'=>'1609549160','data'=>$this->getMchUncomplaintCount('1609549160'),'pay_uniacid'=>24],
//            ];
//        $mch[2] = [
//            // 25=>['mch_id'=>'1620905287','data'=>$this->getMchUncomplaintCount('1620905287'),'pay_uniacid'=>25],
//            // 33=>['mch_id'=>'1623269128','data'=>$this->getMchUncomplaintCount('1623269128'),'pay_uniacid'=>33],
//            77=>['mch_id'=>'1634480723','data'=>$this->getMchUncomplaintCount('1634480723'),'pay_uniacid'=>77],
//            147=>['mch_id'=>'1635811722','data'=>$this->getMchUncomplaintCount('1635811722'),'pay_uniacid'=>147],
//            242=>['mch_id'=>'1636590491','data'=>$this->getMchUncomplaintCount('1636590491'),'pay_uniacid'=>242],
//        ];

//        $mch[0] =$mch[1]  = [];
//        $mch[3] = [
//            35=>['mch_id'=>'1630759818','data'=>$this->getMchUncomplaintCount('1630759818'),'pay_uniacid'=>35],
//            36=>['mch_id'=>'1632092555','data'=>$this->getMchUncomplaintCount('1632092555'),'pay_uniacid'=>36],
//            37=>['mch_id'=>'1634435244','data'=>$this->getMchUncomplaintCount('1634435244'),'pay_uniacid'=>37],
////            38=>['mch_id'=>'111','data'=>$this->getMchUncomplaintCount('1634435244'),'pay_uniacid'=>38],
//
//            78=>['mch_id'=>'1640367186','data'=>$this->getMchUncomplaintCount('1640367186'),'pay_uniacid'=>78],
//            79=>['mch_id'=>'1639874070','data'=>$this->getMchUncomplaintCount('1639874070'),'pay_uniacid'=>78],
//            80=>['mch_id'=>'1642963260','data'=>$this->getMchUncomplaintCount('1642963260'),'pay_uniacid'=>78],
//        ];

        // $mch[0] = [
        //     334=>['mch_id'=>'1645462842','data'=>$this->getMchUncomplaintCount('1645462842'),'pay_uniacid'=>334],
        //     337=>['mch_id'=>'1646557208','data'=>$this->getMchUncomplaintCount('1646557208'),'pay_uniacid'=>337],
        //     3344=>['mch_id'=>'1646558883','data'=>$this->getMchUncomplaintCount('1646558883'),'pay_uniacid'=>3344],
        //     342=>['mch_id'=>'1648322623','data'=>$this->getMchUncomplaintCount('1648322623'),'pay_uniacid'=>342],
        // ];
        $mch[1]  = [
            // 3355=>['mch_id'=>'1645462849','data'=>$this->getMchUncomplaintCount('1645462849'),'pay_uniacid'=>3355],
            // 3377=>['mch_id'=>'1646555768','data'=>$this->getMchUncomplaintCount('1646555768'),'pay_uniacid'=>3377],
            
            
            // 343=>['mch_id'=>'1648755725','data'=>$this->getMchUncomplaintCount('1648755725'),'pay_uniacid'=>343],
            // 341=>['mch_id'=>'1649444259','data'=>$this->getMchUncomplaintCount('1649444259'),'pay_uniacid'=>341],
            // 340=>['mch_id'=>'1649512544','data'=>$this->getMchUncomplaintCount('1649512544'),'pay_uniacid'=>340],
            
            // 349=>['mch_id'=>'1649918888','data'=>$this->getMchUncomplaintCount('1649918888'),'pay_uniacid'=>349],
            // 348=>['mch_id'=>'1650301095','data'=>$this->getMchUncomplaintCount('1650301095'),'pay_uniacid'=>348],
            
            // 347=>['mch_id'=>'1650536101','data'=>$this->getMchUncomplaintCount('1650536101'),'pay_uniacid'=>347],
            
            419=>['mch_id'=>'1656527643','data'=>$this->getMchUncomplaintCount('1656527643'),'pay_uniacid'=>419],
            429=>['mch_id'=>'1661962275','data'=>$this->getMchUncomplaintCount('1661962275'),'pay_uniacid'=>429],
        ];
        // $mch[2]  = [];
        // $mch[3] = [
        //     335=>['mch_id'=>'1645462849','data'=>$this->getMchUncomplaintCount('1645462849'),'pay_uniacid'=>335],
        //     336=>['mch_id'=>'1645447933','data'=>$this->getMchUncomplaintCount('1645447933'),'pay_uniacid'=>336],
        //     337=>['mch_id'=>'1646555768','data'=>$this->getMchUncomplaintCount('1646555768'),'pay_uniacid'=>337],
        // ];

        //76260AB9E006B759C24F0440F009F038B4E83E45
        $this->assign([
            'mch'=>$mch,
        ]);
        return view();
    }
    
    public function getMchUncomplaintCount($mchId)
    {
        return ['uncomplaint'=> db('datatable_complaints')->where('mch_id',$mchId)->where('complaint_state','<>','已处理完成')->count() ];
    }
    
    public function index()
    {
        $mchId = input('mch_id');
        if(input('page'))
        {
            $this->syncListData($mchId);
            $list = db('datatable_complaints')->where('mch_id',$mchId)->page(input('page'),input('limit'))->orderRaw(' CONVERT( complaint_state USING gbk )  ASC')->order('complaint_time DESC')->select();
            $count = db('datatable_complaints')->where('mch_id',$mchId)->count();
            return ['code'=>0,'count'=>$count,'data'=>$list,'msg'=>''];
        }
        return view();
    }
    
    public function view()
    {
        $complaintId = input('complaint_id');
        $id = input('id');
        //$data = db('datatable_complaints')->where('id',$id)->find();
        $this->syncOneData( input('complaint_id'), input('mch_id'));
        $data = db('datatable_complaints')->where('id',$id)->find();
        $data['complaint_order_info'] = json_decode($data['complaint_order_info'],true);
        $data['service_order_info'] = json_decode($data['service_order_info'],true);
        $data['complaint_media_list'] = json_decode($data['complaint_media_list'],true);
        $data['negotiation_historys'] = json_decode($data['negotiation_historys'],true);
        $this->assign([
            'data'=>$data,
        ]);
        return view();
    }
    
    public function response()
    {
        if(request()->isAjax() )
        {
            $data = input('post.');
            //$data = input();
            $WechatPayV3 = new \WechatPayV3\WechatPayV3();
            $result = $WechatPayV3->responseComplaint($data['complaint_id'],$data['response_content'],$data['mch_id']);
            return $result;
        }
        else
        {
            $id = input('id');
            $this->syncOneData( input('complaint_id'), input('mch_id'));
            $data = db('datatable_complaints')->where('id',$id)->find();
            $data['complaint_order_info'] = json_decode($data['complaint_order_info'],true);
            $data['service_order_info'] = json_decode($data['service_order_info'],true);
            $data['complaint_media_list'] = json_decode($data['complaint_media_list'],true);
            $data['negotiation_historys'] = json_decode($data['negotiation_historys'],true);
            $this->assign([
                'data'=>$data,
            ]);
            return view();
        }
    }
    
    public function complete()
    {
        $mchId = input('mch_id');
        $complaintId = input('complaint_id');
        $WechatPayV3 = new \WechatPayV3\WechatPayV3();
        $result = $WechatPayV3->completeComplaint($complaintId,$mchId);

        return $result;
    }
    
    private function syncListData($mchId)
    {
        //$mchid = input('mchid');
        $WechatPayV3 = new \WechatPayV3\WechatPayV3();
        $result = $WechatPayV3->complaints($mchId);
        
        $result = json_decode($result,true)['data'];
        $problemType = ['OTHERS'=>'其他类型','REFUND'=>'退款类型的问题投诉','SERVICE_NOT_WORK'=>'服务权益未生效'];
        $complaintState = ['PENDING'=>'待处理','PROCESSING'=>'处理中','PROCESSED'=>'已处理完成'];

        foreach ($result as $k=>$v)
        {
            $transaction_id = $v['complaint_order_info'][0]['transaction_id'];
            $v['complaint_order_info'] = json_encode($v['complaint_order_info']);
            $v['service_order_info'] = json_encode($v['service_order_info']);
            $v['complaint_media_list'] = json_encode($v['complaint_media_list']);
            $v['problem_type'] = $problemType[$v['problem_type']];
            $v['complaint_state'] = $complaintState[$v['complaint_state']];
            $v['incoming_user_response'] = $v['incoming_user_response']?'是':'否';
            $v['update_time'] = time();
            $v['mch_id'] = $mchId;
            $data = db('datatable_complaints')->where('complaint_id',$v['complaint_id'])->find();
            if(!$data['live_id'])
            {
                $v['live_id'] = Db::connect("new_live_db_config")->table('ims_wzbagent_giftlogs')->where('transaction_id',$transaction_id)->value('live_id');
            }
                
            if($data)
            {
                db('datatable_complaints')->where('complaint_id',$v['complaint_id'])->update($v);
            }
            else
            {
                
                $v['add_time'] = time();
                db('datatable_complaints')->where('complaint_id',$v['complaint_id'])->insertGetId($v);
            }
        }
        return 1;
    }
    
    private function syncOneData($complaintId,$mchId)
    {
        $WechatPayV3 = new \WechatPayV3\WechatPayV3();
        $result = $WechatPayV3->negotiationHistorys($complaintId,$mchId);
        db('datatable_complaints')->where('complaint_id',$complaintId)->update(['negotiation_historys'=>$result]);
    }
    
}



